package com.waypolice.manager.controller;

import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.manager.service.ArchivesKeepingStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 档案保管情况统计
 * @author lzh
 * @date 2019/04/01
 *
 */
@Controller
@RequestMapping("/manage/keeping")
public class ArchivesKeepingStatisticController {

    @Autowired
    private ArchivesKeepingStatisticService archivesKeepingStatisticService;

    @RequestMapping("/statistic")
    public String index(){
        return "/countManager/keepArchiveCount";
    }

    @RequestMapping("/value")
    @ResponseBody
    public Object statisticList(String firstDate,String lastDate){

        //档案类型，0表示所有类型，其他值表示具体的类型
//        String type ="all";
//        String num = "all";
        //归档时间 1
        Date date1 = null;
        //归档时间 2
        Date date2 = new Date();
        if (!firstDate.equals("")){
            date1 = DateTimeUtil.strToDate(firstDate);
        }
        if (!lastDate.equals("")){
            date2 = DateTimeUtil.strToDate(lastDate);
        }

        //标记使用哪个SQL查询 1 表示查询卷表，2 表示查询单件表
        String sign ;
        // 标识档案的是否是1鉴定中，3已移交，8已销毁,0表示所有状态，
        List<Integer> archSigns = new ArrayList<>();
        archSigns.add(0);
        archSigns.add(1);
        archSigns.add(3);
        archSigns.add(8);
        List<Map> list = new ArrayList<>();
        //if (num.equals(type)){
            List<Map> strings = archivesKeepingStatisticService.selectTypes();
            if (!strings.isEmpty()){
                for (Map s:strings){

                    Map<String, Object> stringObjectMap = returnMap();
                    int totalStr = 0;
                    int totalCount = 0;
                    stringObjectMap.put("type",s.get("TYPE"));
                    //所有档案类型，直接对展示内容数量进行拼接
                    for (int i = 0; i < archSigns.size();i++) {
                        //卷返回的集合
                        sign = "1";
                        List<Map> maps = archivesKeepingStatisticService.keepingStatistic((String) s.get("TYPE"),date1,date2,sign,archSigns.get(i));
                        if (!maps.isEmpty()){
                            Map map = maps.get(0);
                            if (map.get("TOTALCOUNT") != null){
                                stringObjectMap.put("str"+i,map.get("TOTALCOUNT"));
                                totalStr += Integer.parseInt(map.get("TOTALCOUNT").toString());
                            }else{
                                stringObjectMap.put("str"+i,0);
                                stringObjectMap.put("count"+i,0);
                            }
                            if (map.get("VOLITEMNUMBER") != null){
                                stringObjectMap.put("count"+i,map.get("VOLITEMNUMBER"));
                                totalCount += Integer.parseInt(map.get("VOLITEMNUMBER").toString());
                            }

                        }
                        //单件返回的集合
                        sign = "2";
                        List<Map> maps1 = archivesKeepingStatisticService.keepingStatistic((String) s.get("TYPE"),date1,date2,sign,archSigns.get(i));
                        if (!maps1.isEmpty()){
                            Map map = maps1.get(0);
                            if (map.get("TOTALCOUNT") != null){
                                boolean b = stringObjectMap.containsKey("count" + i);
                                totalCount += Integer.parseInt(map.get("TOTALCOUNT").toString());
                                if (b){
                                    int count = Integer.parseInt(stringObjectMap.get("count"+i).toString()) + Integer.parseInt(map.get("TOTALCOUNT").toString());
                                    stringObjectMap.put("count"+i,count);
                                }else{
                                    stringObjectMap.put("count"+i,Integer.parseInt(map.get("TOTALCOUNT").toString()));
                                }
                            }
                        }
                    }
                    stringObjectMap.put("totalStr",totalStr);
                    stringObjectMap.put("totalCount",totalCount);
                    list.add(stringObjectMap);
                }

            }

        //}
        /*else{
            Map<String,Object> resultMap = returnMap();
            //表明指定档案类型，直接对展示内容数量进行拼接
            for (int i = 0; i < archSigns.size();i++){
                //卷返回的集合
                sign = "1";
                List<Map> maps = archivesKeepingStatisticService.keepingStatistic(type, date1, date2, sign, archSigns.get(i));
                if (!maps.isEmpty()){
                    Map map = maps.get(0);
                    if (map.get("totalCount") != null){
                        resultMap.put("str"+i,map.get("totalCount"));
                        resultMap.put("type",map.get("archiveType"));
                    }else{
                        resultMap.put("str"+i,0);
                        resultMap.put("count"+i,0);
                    }
                    if (map.get("volItemNumber") != null){
                        resultMap.put("count"+i,map.get("volItemNumber"));
                    }

                }
                //单件返回的集合
                sign = "2";
                List<Map> maps1 = archivesKeepingStatisticService.keepingStatistic(type, date1, date2, sign,archSigns.get(i));
                if (!maps1.isEmpty()){
                    Map map = maps1.get(0);
                    if (map.get("totalCount") != null){
                        resultMap.put("count"+i,((int)resultMap.get("count"+i) + (int)map.get("totalCount")) );
                    }
                }

            }
            list.add(resultMap);
        }*/
        Map resultMap = new HashMap();
        if (!list.isEmpty()){
            resultMap.put("total",list.size());
        }else{
            resultMap.put("total",0);
        }
        resultMap.put("rows",list);
        return resultMap;
    }

    public Map<String,Object> returnMap(){
        Map<String,Object> resultMap = new HashMap<>();
//        resultMap.put("str1",0);
//        resultMap.put("count1",0);
//        resultMap.put("str2",0);
//        resultMap.put("count2",0);
//        resultMap.put("str3",0);
//        resultMap.put("count3",0);
//        resultMap.put("str4",0);
//        resultMap.put("count4",0);
        return resultMap;
    }
}